// pages/course/video/videoItem/videoItem.js
var util = require("../../../utils/util.js");

Page({

  /**
   * 页面的初始数据
   */
  data: {
    // navs-begin
    navsChecked: ["navs-checked", ""],
    commentHidden: true,
    detailHidden: false,
    //navs -end

    detail: {},
    CommList: [],
    rlvCourseList: [],
    videoUrl: "",
    videoHeight: 320,
    scrollHeight:756
  },

  // navs-begin


  onNavsChecked: function (e) {
    var id = parseInt(e.target.dataset.id);
    if (id == 0) {
      this.setData({
        detailHidden: false,
        commentHidden: true
      });
    } else {
      this.setData({
        detailHidden: true,
        commentHidden: false
      });
    }
    var navsChecked = this.data.navsChecked;
    for (let i = 0; i < navsChecked.length; i++) {
      if (id == i) {
        navsChecked[i] = "navs-checked";
      } else {
        navsChecked[i] = "";
      }
    }
    this.setData({
      navsChecked: navsChecked
    });

  },
  // navs-end


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    wx.showLoading({
      title: '稍等',
    })
    //set scrollHeight
    var scrollHeight = getApp().globalData.windowHeight * 0.45 * 2;
    this.setData({
      scrollHeight: scrollHeight
    });
    //set videoContainer height
    var videoHeight = getApp().globalData.windowWidth * 3 / 4 * 2;
    this.setData({
      videoHeight: videoHeight
    });

    //get detail
    var MsgID = options.MsgID;
    var Token = "";
    var page = this;
    var userId = getApp().globalData.userId;
    if (userId == "") {
      setTimeout(function () {
        wx.navigateBack({
          delta: 1,
        });
      }, 1000);
      wx.showToast({
        title: '请先登录',
      });
    }
    var basePath = getApp().globalData.basePath.miniAppPath;
    var sessionId = getApp().globalData.sessionId;
    wx.request({
      url: basePath + "/wxMa/3/" + userId,
      header: {
        'content-type': 'application/x-www-form-urlencoded',
        'Cookie': "JSESSIONID=" + sessionId
      },
      success: function (res) {
        if (res.data.code == "21") {
          Token = res.data.token;
          var params = {
            Param: {
              MsgID: MsgID,
              Token: Token
            },
            Action: 6212
          };
          util.reqMedia(params, function (value) {
            var detail = value;
            var typeName = value.TypeName;
            detail.TypeName = util.sToN(typeName);
            // get media url
            var VideoID = detail.MediaID;
            params = {
              Param: {
                VideoID: VideoID
              },
              Action: 4120
            };
            util.reqMedia(params, function (value) {
              page.setData({
                videoUrl: value.VideoPath
              });
            });
            page.setData({
              detail: detail
            });
            wx.hideLoading();
          });
        }
      }
    });

    // get comment
    var params = {
      Param: {
        MsgID: MsgID,
        PageIndex: 1,
        ShowIndexNum: 50
      },
      Action: 6230
    };
    util.reqMedia(params, function (value) {
      page.setData({
        CommList: value.CommList
      });
    });

    //get rlvCourseList
    params = {
      Param: {
        BusinessID: MsgID,
        PageIndex: 1,
        ShowIndexNum: 50
      },
      Action: 18025
    };
    util.reqMedia(params, function (value) {
      page.setData({
        rlvCourseList: value.PackageList
      });
    });

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
          this.setData({
            videoUrl:""
          });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showLoading({
      title: 'loading'
    });
    setTimeout(function () {
      wx.hideLoading();
      wx.stopPullDownRefresh();
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    wx.showLoading({
      title: '到底了',
    });

    setTimeout(function () {
      wx.hideLoading();
    }, 500);

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})